package com.atguigu.gmall.cart.rpc.controller;


import com.atguigu.gmall.cart.service.CartService;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.result.ResultCodeEnum;
import com.atguigu.gmall.common.util.AuthContextHolder;
import com.atguigu.gmall.model.bo.UserIdBo;
import com.atguigu.gmall.model.order.OrderDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/api/inner/cart")
@RestController
public class CartOrderController {


    @Autowired
    CartService cartService;

    @PostMapping("/clear/cart/items")
    public Result clearCartItems(@RequestBody List<Long> skuIds){
        UserIdBo idBo = AuthContextHolder.getUserIdBo();
        String cartKey = cartService.determinCartKey(idBo.getUserId(), idBo.getUserTempId());
        cartService.clearCartItems(cartKey,skuIds);
        return Result.ok();
    }

    /**
     * 获取购物车中要参与生成订单的所有商品
     * @return
     */
    @GetMapping("/order/infos")
    public Result<List<OrderDetail>> getCartInfoForOrder(){
        //1、获取当前登录的用户信息
        UserIdBo bo = AuthContextHolder.getUserIdBo();


        if(bo.getUserId() == null){
            return Result.build(null, ResultCodeEnum.LOGIN_AUTH);
        }

        //2、拿到购物车键
        String cartKey = cartService.determinCartKey(bo.getUserId(), bo.getUserTempId());
        //3、用户登录了，获取用户购物车中所有选中的商品
        List<OrderDetail> checkedItems = cartService.getLoginCartCheckedItem(cartKey);

        return Result.ok(checkedItems);
    }
}
